import { useState, useEffect } from 'react';
import useDebounceFn from '../use-debounce-fn';
import { DebounceOptions } from './debounceOptions';

export default <T>(value: T, options?: DebounceOptions) => {
    const [debounced, setDebounced] = useState(value)

    const { run } = useDebounceFn(() => {
        setDebounced(value)
    }, options)

    useEffect(() => {
        run()
    }, [value])

    return debounced
}